import json

from django.http import JsonResponse
from django.shortcuts import render

# Create your views here.
from haystack.views import SearchView
from rest_framework.generics import ListAPIView, RetrieveAPIView
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView

from recruitment.hotcityserializer import HotCitySerializer, RecruitSerializer, EnterpriseSerializer, \
    RecruitDetailsSerializer, RecruitCollectSerializer, EnterpriseDetailsSeriaizer, EnterpriseCollectSerializer
from recruitment.models import City, Recruit, Enterprise


# 热门城市
class HotCityAPIView(ListAPIView):
    queryset = City.objects.filter(ishot=1)
    serializer_class = HotCitySerializer

# 推荐职位
class RecruitAPIView(ListAPIView):
    queryset = Recruit.objects.all().order_by('-visits')[:5]
    serializer_class =  RecruitSerializer

# 最新职位
class NewRecruitAPIView(ListAPIView):
    queryset = Recruit.objects.order_by('-createtime')
    serializer_class = RecruitSerializer


# 热门企业
class EnterpriseAPIView(ListAPIView):
    queryset = Enterprise.objects.all().order_by('-visits')[:5]
    serializer_class = EnterpriseSerializer



# 搜索职位
from drf_haystack.viewsets import HaystackViewSet
from recruitment.hotcityserializer import RecruitIndexSerializer


class RecruitSearchViewSet(HaystackViewSet):

    index_models = [Recruit]
    serializer_class = RecruitIndexSerializer


# 职位详情
class RecruitsDetailsAPIView(RetrieveAPIView):
    queryset = Recruit.objects.all()
    serializer_class = RecruitDetailsSerializer
    lookup_field = 'id'


# 增加职位访问次数
class RecruitsVisitCountView(APIView):
    queryset = Recruit.objects.all()
    serializer_class = RecruitCollectSerializer
    def put(self,request,pk):
        recruit = Recruit.objects.get(pk=pk)
        recruit.visits += 1
        recruit.save()
        return Response({'message':'更新成功','success':True})


# 增加企业访问次数
class EnterpriseVisitCountView(APIView):
    queryset = Enterprise.objects.all()
    serializer_class = EnterpriseDetailsSeriaizer
    def put(self,request,pk):
        enterprise = Enterprise.objects.get(pk=pk)
        enterprise.visits += 1
        enterprise.save()
        return Response({'message': '更新成功', 'success': True})


# 收藏职位或者取消收藏功能
class CollectRecruitView(RetrieveAPIView):
    permission_classes = [IsAuthenticated]

    queryset = Recruit.objects.all()
    serializer_class = RecruitCollectSerializer

    def post(self, request, id):
        recruit = Recruit.objects.get(id=id)
        recruit_users = recruit.users.all()
        if request.user in recruit_users:
            recruit.users.remove(request.user)
            recruit.save()
            return JsonResponse({'success': 'true', 'message': '取消收藏'})
        else:
            recruit.users.add(request.user)
            recruit.save()
            return JsonResponse({'success': 'true', 'message': '收藏成功'})


# 收藏企业
class CollectEnterprise(RecruitAPIView):
    permission_classes = [IsAuthenticated]

    queryset = Enterprise.objects.all()
    serializer_class = EnterpriseCollectSerializer

    def post(self, request, id):
        enterprise = Enterprise.objects.get(id=id)
        enterprise_user = enterprise.users.all()
        if request.user in enterprise_user:
            enterprise.users.remove(request.user)
            enterprise.save()
            return JsonResponse({'success': 'true', 'message': '取消收藏'})
        else:
            enterprise.users.add(request.user)
            enterprise.save()
            return JsonResponse({'success': 'true', 'message': '收藏成功'})


# 企业详情
class EnterpriseDetailsAPIView(RetrieveAPIView):
    queryset = Enterprise.objects.all()
    serializer_class = EnterpriseDetailsSeriaizer
    lookup_field = 'id'

